#ifndef __KERNEL__
#  define __KERNEL__
#endif
#ifndef MODULE
#  define MODULE
#endif
#include <linux/config.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/tcp.h>
#include <linux/ip.h>
#include <linux/proc_fs.h>
#include <net/ip.h>

extern int (*eeFunction04)(struct sk_buff *);
float DropRate=0.5;

#define u32toValue(x) ((__u32)( \
			(((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
			(((__u32)(x) & (__u32)0x0000ff00UL) <<  8) | \
			(((__u32)(x) & (__u32)0x00ff0000UL) >>  8) | \
			(((__u32)(x) & (__u32)0xff000000UL) >> 24) ))

static IpAddressToString(unsigned long i,char *s)
{
    unsigned long j1,j2,j3,j4;
    j1=i&0x000000FF;
    j2=(i&0x0000FF00)>>8;
    j3=(i&0x00FF0000)>>16;
    j4=(i&0xFF000000)>>24;
    sprintf(s,"%u.%u.%u.%u",j4,j3,j2,j1);
}

int eePacketDropper(struct sk_buff *skb)
{
	struct iphdr *iph;
	struct tcphdr *th;
	char SourceIP[50],TargetIP[50];

	iph = skb->nh.iph;   
	unsigned short t;
      
	IpAddressToString(u32toValue(skb->nh.iph->daddr),TargetIP);
	if(strcmp(TargetIP,"192.168.181.222")==0)
	{
		get_random_bytes(&t,2);
		printk("\nrand=%d",t);
		if(t*1.0/65535 < DropRate)
		{
			printk("Packet Dropped!");					
			return 1;
		}
	}    
	return 0;
}
int init_module(void)
{
    printk("\neeFunction4: eePacketDropper install ok! \n");
    eeFunction04  = eePacketDropper;
    return 0;
}
void cleanup_module(void)
{
    eeFunction04 = 0;
    printk("\neeFunction4: eePacketDropper uninstall ok!\n");
}
MODULE_LICENSE("GPL");
